//package com.juben.svr.demos.web;
//
//import com.itextpdf.html2pdf.HtmlConverter;
////import com.juben.svr.domain.PdfRequest;
//import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//@RestController
//@CrossOrigin(origins = "http://localhost:5173",allowedHeaders = "*",allowCredentials = "true")
//public class pdftext2 {
//    private static final String PDF_DIRECTORY = "D:/pdfs/pdf"; // 请替换为实际路径
//
//    @PostMapping("/generate-pdf2")
//    public String generatePdf(@RequestBody PdfRequest request) {
//        System.out.println("Received HTML: " + request.getHtml());
//
//        if (request == null || request.getHtml() == null || request.getHtml().trim().isEmpty()) {
//            return "Error: Invalid request or empty HTML content.";
//        }
//
//        // 确保目录存在，如果不存在则创建
//        File pdfDir = new File(PDF_DIRECTORY);
//        if (!pdfDir.exists()) {
//            pdfDir.mkdirs(); // 创建目录
//        }
//
//        // 定义生成的 PDF 文件路径;确保文件名唯一
//        String fileName = "example_" + System.currentTimeMillis() + ".pdf";
//        String filePath = PDF_DIRECTORY + "/" + fileName; // 注意添加路径分隔符
//
//        try (FileOutputStream fos = new FileOutputStream(filePath)) {
//            System.out.println("Generating PDF at==========: " + filePath);
//            // 使用 HtmlConverter 将 HTML 转换为 PDF
//            HtmlConverter.convertToPdf(request.getHtml(), fos);
//            System.out.println("PDF generated successfully----------------.");
//            return "PDF generated successfully. Saved at: " + filePath;
//
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "Error generating PDF: I/O error - " + e.getMessage();
//        }
//    }
//}
